package ynu;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SaleData {
    private String movieName; // 影片名称
    private String theaterName; // 放映厅名称
    private Date showTime; // 放映时间
    private String seatNumber; // 座位号
    private String ticketId; // 电子票号
    private double price; // 票价
    private Date saleTime; // 销售时间
    private boolean isRefunded; // 是否退票

    public SaleData(String movieName, String theaterName, Date showTime, String seatNumber,
                    String ticketId, double price, Date saleTime, boolean isRefunded) {
        this.movieName = movieName;
        this.theaterName = theaterName;
        this.showTime = showTime;
        this.seatNumber = seatNumber;
        this.ticketId = ticketId;
        this.price = price;
        this.saleTime = saleTime;
        this.isRefunded = isRefunded;
    }

    // 读取所有销售数据
    public static List<SaleData> readSaleData() {
        List<SaleData> sales = new ArrayList<>();
        File file = new File("D:\\java实训大作业\\销售数据.txt");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        if (!file.exists()) {
            return sales;
        }

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split("\\|");
                if (parts.length == 8) {
                    try {
                        sales.add(new SaleData(parts[0], parts[1],
                                sdf.parse(parts[2]), parts[3],
                                parts[4], Double.parseDouble(parts[5]),
                                sdf.parse(parts[6]), Boolean.parseBoolean(parts[7])));
                    } catch (ParseException e) {
                        System.out.println("解析日期出错: " + e.getMessage());
                    }
                }
            }
        } catch (IOException e) {
            System.out.println("读取销售数据出错: " + e.getMessage());
        }
        return sales;
    }

    // 保存所有销售数据
    public static void saveSaleData(List<SaleData> sales) {
        File dir = new File("D:\\java实训大作业");
        if (!dir.exists()) {
            dir.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(
                "D:\\java实训大作业\\销售数据.txt"))) {
            for (SaleData sale : sales) {
                bw.write(sale.movieName + "|" + sale.theaterName + "|" +
                        sdf.format(sale.showTime) + "|" + sale.seatNumber + "|" +
                        sale.ticketId + "|" + sale.price + "|" +
                        sdf.format(sale.saleTime) + "|" + sale.isRefunded);
                bw.newLine();
            }
        } catch (IOException e) {
            System.out.println("保存销售数据出错: " + e.getMessage());
        }
    }

    // getter方法
    public String getMovieName() {return movieName; }
    public String getTheaterName() { return theaterName; }
    public Date getShowTime() { return showTime; }
    public String getSeatNumber() { return seatNumber; }
    public String getTicketId() { return ticketId; }
    public double getPrice() { return price; }
    public Date getSaleTime() { return saleTime; }
    public boolean isRefunded() { return isRefunded; }

    // setter方法
    public void setRefunded(boolean refunded) { isRefunded = refunded; }
}
